package com.trickytriads.services.remote
{
	import com.adobe.serialization.json.JSON;
	import com.trickytriads.events.LoadNewsEvent;
	import com.trickytriads.model.NewsData;
	import com.trickytriads.services.FacebookService;
	import com.trickytriads.services.interfaces.I_NewsService;
	
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.mxml.HTTPService;

	/**
	 * Remote news service.
	 **/
	[Event(name="loadGeneric", type="com.trickytriads.events.LoadingEvent")]
	[Event(name="newsLoadDone", type="com.trickytriads.events.LoadNewsEvent")]	
	[ManagedEvents("newsLoadDone, loadGeneric")]	 
	public class NewsServiceImpl_Remote extends EventDispatcher implements I_NewsService
	{
		// Inject in the server that we're calling for this stuff.
		[Inject]
		public var httpService:HTTPService;
		
		public function NewsServiceImpl_Remote(target:IEventDispatcher=null)
		{
			super(target);
		}
		
		/**		 
		 * Loads a news item from the remote server.
		 **/
		public function loadNewsItem(newsid:int):void
		{			
			var sendObj:Object = FacebookService.makeFacebookObject();
			sendObj.NEWSITEM = "true";
			sendObj.news_id = newsid;
			httpService.addEventListener(ResultEvent.RESULT, loadNewsItemResult);
			httpService.addEventListener(FaultEvent.FAULT, loadNewsItemFault);
			httpService.send(sendObj);
		}
		
		/**
		 * Result handler for loadNewsItem
		 **/
		public function loadNewsItemResult(result:ResultEvent) : void
		{
			// Convert to generic Object class
			var resultObject:Object = JSON.decode(String(result.result));
			if (resultObject.hasOwnProperty("I_AM_ERROR"))
			{
				// Alert an error message for now
				Alert.show("Error!!");				
			}
			else
			{						
				// Make a NewsData object
				var received:NewsData = new NewsData(resultObject.news_id, new Date(2010, 6, 21), resultObject.author, resultObject.text, resultObject.title);
				// Make a LoadNewsEvent of type NEWS_LOAD_DONE to tell the world we're done
				var receivedEvent:LoadNewsEvent = new LoadNewsEvent(LoadNewsEvent.NEWS_LOAD_DONE, 0);
				receivedEvent.news = received;
				// Now dispatch the event, parsley will inject it into the system.
				trace("Dispatching the LoadNewsEvent");
				dispatchEvent(receivedEvent);
			}
			// Remove listeners
			httpService.removeEventListener(ResultEvent.RESULT, loadNewsItemResult);
			httpService.removeEventListener(FaultEvent.FAULT, loadNewsItemFault);
		}
		
		/**
		 * Fault handler for loadNewsItem
		 **/		
		public function loadNewsItemFault(fault:FaultEvent) : void
		{
			Alert.show("Fault: " + fault.toString());
			// Remove listeners
			httpService.removeEventListener(ResultEvent.RESULT, loadNewsItemResult);
			httpService.removeEventListener(FaultEvent.FAULT, loadNewsItemFault);			
		}
		
		public function loadAllNews():void
		{
		}
		
	}
}